package com.app.micaihu.custom.view;

import android.content.Context;
import android.graphics.drawable.Animatable;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.app.photodraweeview.OnViewTapListener;
import com.app.photodraweeview.PhotoDraweeView;
import com.app.utils.util.image.LoadImageUtils;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.controller.BaseControllerListener;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.image.ImageInfo;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;

/**
 * Created by Guo on 2017/4/21.
 *
 */

public class PhotoImageView extends FrameLayout implements View.OnLongClickListener, View.OnClickListener, OnViewTapListener {
    private Context context;
    private ImagePhotoViewListener imagePhotoViewListener;

    public PhotoImageView(Context context) {
        this(context, null);
    }

    public PhotoImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PhotoImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {


    }
    public void loadImage(final String url,Context context) {
        this.context = context;
        Uri uri;
        if (url.startsWith("/storage/") || url.startsWith("/data")) {
            uri = Uri.parse("file://" + getContext().getPackageName() + "/" + url);
        } else {
            uri = Uri.parse(url);
        }
        final PhotoDraweeView photoDraweeView = new PhotoDraweeView(context);

        ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                .setResizeOptions(new ResizeOptions(getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels))
                .build();
        photoDraweeView.setHierarchy(photoDraweeView.getLocalGenericDraweeHierarchy());
        photoDraweeView.setController(Fresco.newDraweeControllerBuilder()
                .setOldController(photoDraweeView.getController())
                .setImageRequest(request)
                .setAutoPlayAnimations(true)
                .setControllerListener(new BaseControllerListener<ImageInfo>() {
                    @Override
                    public void onFailure(String id, Throwable throwable) {
                        super.onFailure(id, throwable);
                        photoDraweeView.setEnableDraweeMatrix(false);
                    }
                    @Override
                    public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) {
                        super.onFinalImageSet(id, imageInfo, animatable);
//                        Log.e("gzq", "width====>" + imageInfo.getWidth() + "height====>" + imageInfo.getHeight());
                        photoDraweeView.setEnableDraweeMatrix(true);
                        photoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
                        if ((imageInfo.getHeight() / imageInfo.getWidth() > 4)
                                &&!url.endsWith(".gif")) {
                            removeView(photoDraweeView);
                            addLongImageView(url);
                        }else{
                            photoDraweeView.setOnLongClickListener(PhotoImageView.this);
                            photoDraweeView.setOnViewTapListener(PhotoImageView.this);
                        }
                    }
                    @Override
                    public void onIntermediateImageFailed(String id, Throwable throwable) {
                        super.onIntermediateImageFailed(id, throwable);
                        photoDraweeView.setEnableDraweeMatrix(false);
                    }
                    @Override
                    public void onIntermediateImageSet(String id, ImageInfo imageInfo) {
                        super.onIntermediateImageSet(id, imageInfo);
                        photoDraweeView.setEnableDraweeMatrix(true);
                        if (imageInfo != null) {
                            photoDraweeView.update(imageInfo.getWidth(), imageInfo.getHeight());
                        }
                    }

                })
                .build());

        addView(photoDraweeView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    private void addLongImageView(String url) {
        SubsamplingScaleImageView subsamplingScaleImageView = new SubsamplingScaleImageView(context);
        subsamplingScaleImageView.setOnLongClickListener(this);
        subsamplingScaleImageView.setOnClickListener(this);
        LoadImageUtils.getInstance().loadLongImage(subsamplingScaleImageView,url);
        addView(subsamplingScaleImageView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    @Override
    public boolean onLongClick(View v) {
        if(imagePhotoViewListener != null){
            imagePhotoViewListener.onLongStepClick();
        }
        return false;
    }

    @Override
    public void onClick(View v) {
        if(imagePhotoViewListener != null){
            imagePhotoViewListener.onOneStepClick();
        }
    }
    public  void setImagePhotoViewListener(ImagePhotoViewListener imagePhotoViewListener){
        this.imagePhotoViewListener = imagePhotoViewListener;
    }
    @Override
    public void onViewTap(View view, float x, float y) {
        if(imagePhotoViewListener != null){
            imagePhotoViewListener.onOneStepClick();
        }
    }
    public interface ImagePhotoViewListener{
        void onOneStepClick();

        void onLongStepClick();
    }



}
